package com.example.zhangyu.myapplication.adapter;

/**
 * Created by luoxw on 2016/6/20.
 */
/*
public class ListAdapter extends BaseAdapter {
    Context context;

    List<String> list;

    public ListAdapter(Context context, List<String> list) {
        this.context = context;
        this.list = list;
    }


    @Override
    public int getCount() {
        return list.size();
    }

    @Override
    public Object getItem(int position) {
        return list.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {

       ViewHolder holder = null;

       if  (convertView == null) {
                   convertView = LayoutInflater.from(context).inflate(R.layout.item_mylist, parent, false); //加载布局

//        return null;
      */
/*
        48         if (convertView == null) {
            49             convertView = mInflater.inflate(R.layout.item_listview, parent, false); //加载布局
            50             holder = new ViewHolder();
            51
            52             holder.titleTv = (TextView) convertView.findViewById(R.id.titleTv);
            53             holder.descTv = (TextView) convertView.findViewById(R.id.descTv);
            54             holder.timeTv = (TextView) convertView.findViewById(R.id.timeTv);
            55             holder.phoneTv = (TextView) convertView.findViewById(R.id.phoneTv);
            56
            57             convertView.setTag(holder);
            58         } else {   //else里面说明，convertView已经被复用了，说明convertView中已经设置过tag了，即holder
            59             holder = (ViewHolder) convertView.getTag();
            60         }
        61
        62         Bean bean = mDatas.get(position);
        63         holder.titleTv.setText(bean.getTitle());
        64         holder.descTv.setText(bean.getDesc());
        65         holder.timeTv.setText(bean.getTime());
        66         holder.phoneTv.setText(bean.getPhone());
        67
        68         return convertView;
        69     }*//*



        return null;
           class ViewHolder {
               TextView titleTv;
               TextView descTv;
               TextView timeTv;
               TextView phoneTv;
           }
    }


}
*/

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.example.zhangyu.myapplication.R;

import java.util.List;



/**
 * Created by smyhvae on 2015/5/4.
 */
class MyAdapter extends BaseAdapter {
    private LayoutInflater mInflater;
    private List<String> mDatas;
    Context context;

    //MyAdapter需要一个Context，通过Context获得Layout.inflater，然后通过inflater加载item的布局
    public MyAdapter(Context context, List<String> datas) {

        mInflater = LayoutInflater.from(context);
        this.context = context;
        mDatas = datas;
    }

    //返回数据集的长度
    @Override
    public int getCount() {
        return mDatas.size();
    }

    @Override
    public Object getItem(int position) {
        return mDatas.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    //这个方法才是重点，我们要为它编写一个ViewHolder
    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder = null;
        if (convertView == null) {
            convertView = mInflater.inflate(R.layout.item_mylist, parent, false); //加载布局
            holder = new ViewHolder();
            holder.textView= (TextView) convertView.findViewById(R.id.textView);
/*
            holder.titleTv = (TextView) convertView.findViewById(R.id.titleTv);
            holder.descTv = (TextView) convertView.findViewById(R.id.descTv);
            holder.timeTv = (TextView) convertView.findViewById(R.id.timeTv);
            holder.phoneTv = (TextView) convertView.findViewById(R.id.phoneTv);*/

            convertView.setTag(holder);
        } else {   //else里面说明，convertView已经被复用了，说明convertView中已经设置过tag了，即holder
            holder = (ViewHolder) convertView.getTag();
        }

       /* Bean bean = mDatas.get(position);
        holder.titleTv.setText(bean.getTitle());
        holder.descTv.setText(bean.getDesc());
        holder.timeTv.setText(bean.getTime());
        holder.phoneTv.setText(bean.getPhone());*/

        return convertView;
    }

    //这个ViewHolder只能服务于当前这个特定的adapter，因为ViewHolder里会指定item的控件，不同的ListView，item可能不同，所以ViewHolder写成一个私有的类
    private class ViewHolder {
        TextView textView;
        TextView descTv;
        TextView timeTv;
        TextView phoneTv;
    }

}